#include "music.h"
#include "ui_music.h"

Music::Music(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Music)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::Window);

    musicdb = new MusicDB();
    player = new QMediaPlayer(this);
    playlist = new QMediaPlaylist(this);

    connect( playlist, SIGNAL(currentIndexChanged(int)),
             this, SLOT(showMusicName(int)));

    //设置背景图片
    QPalette palette = this->palette();
    palette.setBrush(this->backgroundRole(),QBrush(QPixmap(":/resources/writecar.jpg")));
    this->setPalette(palette);

    this->initMusic();
}

Music::~Music()
{
    delete ui;
}

/**
 * @brief 显示音乐设置
 *
 */
void Music::musicShow()
{
    if (player->state() == QMediaPlayer::StoppedState
            || player->state() == QMediaPlayer::PausedState)
        ui->toolButton->setIcon(QIcon(":/resources/music_4.png"));
    else
        ui->toolButton->setIcon(QIcon(":/resources/music_3.png"));

    UsersDB *userdb = new UsersDB;
    UserModel model = userdb->getUserInfo();
    int fontsize = userdb->getFontSize(model.name);
    //设置事例字体点阵
    QFont font;
    font.setPointSize(fontsize);
    this->ui->label->setFont(font);

    this->show();
}

/**
 * @brief 返回
 *
 */
void Music::on_toolButton_8_clicked()
{
    switch (StatusValue::status)
    {
    case Load:
        break;
    case Ready:
        this->hide();
        emit enterBegin();
        break;
    case Playing:
        break;
    case Pausing:
        this->hide();
        emit enterPause();
    default:
        break;
    }
}

/**
 * @brief 播放或暂停音乐
 *
 */
void Music::on_toolButton_clicked()
{
    if (player->state() == QMediaPlayer::StoppedState
            || player->state() == QMediaPlayer::PausedState)
    {
        if ( playlist->isEmpty())
        {
            ui->label->setText("播放列表为空~~");
            return;
        }
        ui->toolButton->setIcon(QIcon(":/resources/music_3.png"));
        player->play();
        ui->label->setText(playlist->media(playlist->currentIndex())
                           .canonicalUrl().fileName());
    }
    else
    {
        ui->toolButton->setIcon(QIcon(":/resources/music_4.png"));
        player->pause();
    }
}

/**
 * @brief 当播放音乐改变时更新显示的歌曲名称
 *
 * @param position
 */
void Music::showMusicName(int position)
{
    if ( playlist->isEmpty())
    {
        ui->label->setText("播放列表为空~~");
        return;
    }
    ui->label->setText(playlist->media(position)
                       .canonicalUrl().fileName());
}

/**
 * @brief 初始化音乐播放
 *
 */
void Music::initMusic()
{
    musicList = musicdb->getMusicPath();
    playlist->setPlaybackMode( QMediaPlaylist::Random);
    player->setPlaylist(playlist);
    foreach (QString music, musicList)
    {
        playlist->addMedia(QUrl::fromLocalFile(music));
    }
}

/**
 * @brief 下一曲
 *
 */
void Music::on_toolButton_4_clicked()
{
    if ( playlist->isEmpty())
    {
        ui->label->setText("播放列表为空~~");
        return;
    }
    playlist->next();
}

/**
 * @brief 上一曲
 *
 */
void Music::on_toolButton_3_clicked()
{
    if ( playlist->isEmpty())
    {
        ui->label->setText("播放列表为空~~");
        return;
    }
    playlist->previous();
}

/**
 * @brief 停止
 *
 */
void Music::on_toolButton_6_clicked()
{
    ui->toolButton->setIcon(QIcon(":/resources/music_4.png"));
    player->stop();
}

/**
 * @brief 删除当前播放歌曲
 *
 */
void Music::on_toolButton_5_clicked()
{
    musicdb->removeMusic(ui->label->text());
    playlist->removeMedia(playlist->currentIndex());
    this->showMusicName(playlist->currentIndex());
}

/**
 * @brief 本地查找
 *
 */
void Music::on_toolButton_7_clicked()
{
    this->musicdb->searchMusic();
    this->initMusic();
}
